import { _decorator,Color,Component ,Label,Node, Sprite } from 'cc';
import { Core } from '../Core/Core';
import { EView, GeneralLevelToTitle } from '../Config/Enum';


const { ccclass, property } = _decorator;

@ccclass('GeneralView')
export class GeneralView extends Component {


    ndrenown: Node = null!; // 声望节点
    ndOfficalPosition: Node = null!; // 官职节点
    level


    ndFengtiaoyushun: Node = null!; // 官职节点
    ndZhongzhichengcheng: Node = null!; // 官职节点
    ndManzaiergui: Node = null!; // 官职节点
    ndFuzaoshengfeng: Node = null!; // 官职节点
    
    ndWanjianqifa: Node = null!; // 官职节点
    ndShiwanhuoji: Node = null!; // 官职节点
    ndJinjijiagu: Node = null!; // 官职节点
    ndFengyunbianse: Node = null!; // 官职节点

    // 子页面
    ndGeneral: Node = null!; // 将军页
    ndWall: Node = null!; // 城墙页
    ndMartial: Node = null!; // 内政页

    // 子页面按钮节点(要改颜色)
    ndButtons: Node
    ndBtnGeneral: Node = null!; // 将军页
    ndBtnWall: Node = null!; // 城墙页
    ndBtnMartial: Node = null!; // 内政页





    protected onLoad(): void {

        
        this.ndGeneral = this.node.getChildByName("General");
        this.ndWall = this.node.getChildByName("Wall");
        this.ndMartial = this.node.getChildByName("Martial");

        //this.ndBG = this.node.getChildByName("BG");
        this.ndrenown = this.ndGeneral.getChildByName("Renown");
        this.ndOfficalPosition = this.ndGeneral.getChildByName("OfficalPosition");

        this.ndFengtiaoyushun = this.ndGeneral.getChildByName("Skills").getChildByName("Passive_Skills").getChildByName("Fengtiaoyushun");
        this.ndZhongzhichengcheng = this.ndGeneral.getChildByName("Skills").getChildByName("Passive_Skills").getChildByName("Zhongzhichengcheng");
        this.ndManzaiergui = this.ndGeneral.getChildByName("Skills").getChildByName("Passive_Skills").getChildByName("Manzaiergui");
        this.ndFuzaoshengfeng = this.ndGeneral.getChildByName("Skills").getChildByName("Passive_Skills").getChildByName("Fuzaoshengfeng");

        this.ndWanjianqifa = this.ndGeneral.getChildByName("Skills").getChildByName("Active_Skills").getChildByName("Wanjianqifa");
        this.ndShiwanhuoji = this.ndGeneral.getChildByName("Skills").getChildByName("Active_Skills").getChildByName("Shiwanhuoji");
        this.ndJinjijiagu = this.ndGeneral.getChildByName("Skills").getChildByName("Active_Skills").getChildByName("Jinjijiagu");
        this.ndFengyunbianse = this.ndGeneral.getChildByName("Skills").getChildByName("Active_Skills").getChildByName("Fengyunbianse");


        this.ndButtons = this.node.getChildByName("Buttons")
        this.ndBtnGeneral = this.ndButtons.getChildByName("Button_General");
        this.ndBtnWall =  this.ndButtons.getChildByName("Button_Wall");
        this.ndBtnMartial =  this.ndButtons.getChildByName("Button_Martial");
        

        
    }

    protected onEnable(): void {
        //this.ndBG.on(Node.EventType.TOUCH_END, this.onButtonCloseThisWindow, this);

        Core.Instance.currentView = EView.GeneralView;

        this.loadData();

        this.checkUnlockSystem();
        
    }

    protected onDisable(): void {

        //this.ndBG.off(Node.EventType.TOUCH_END, this.onButtonCloseThisWindow, this);
        
    }

    protected start(): void {
        
    }


    loadData(){
        //加载数据
        console.log("GeneralView loadData");
        // 获取level
        this.level = Core.Instance.generalData.level || 1;
        console.log("GeneralView level:", this.level);

        this.ndOfficalPosition.getChildByName("Value").getComponent(Label).string =  GeneralLevelToTitle[this.level] +" (Lv."+this.level.toString()+")";

        this.ndrenown.getChildByName("Value").getComponent(Label).string =  Core.Instance.generalData.renown.toString()+"/"+Core.Instance.generalData.renownToNextLevel.toString();








    }

    checkUnlockSystem(){
        // 检查并解锁系统
        if(this.level >= 2){
            this.ndZhongzhichengcheng.getChildByName("Mask").active = false;
            this.ndJinjijiagu.getChildByName("Mask").active = false;
        }
        if(this.level >= 3){
            this.ndManzaiergui.getChildByName("Mask").active = false;
            this.ndFuzaoshengfeng.getChildByName("Mask").active = false;
        }
        if(this.level >= 4){

            this.ndWanjianqifa.getChildByName("Mask").active = false;
            this.ndShiwanhuoji.getChildByName("Mask").active = false;
        }
        if(this.level >= 5){
            this.ndJinjijiagu.getChildByName("Mask").active = false;
            this.ndFengyunbianse.getChildByName("Mask").active = false;
        }


    }




    onButtonOpenGeneral(){
        console.log("打开将军页")
        this.ndGeneral.active = true;
        this.ndWall.active = false;
        this.ndMartial.active = false;

        this.ndBtnGeneral.getComponent(Sprite).color = Color.WHITE;
        this.ndBtnWall.getComponent(Sprite).color = Color.GRAY;
        this.ndBtnMartial.getComponent(Sprite).color = Color.GRAY;
    }

    onButtonOpenWall(){
        console.log("打开城墙页")
        this.ndGeneral.active = false;
        this.ndWall.active = true;
        this.ndMartial.active = false;
        this.ndBtnGeneral.getComponent(Sprite).color = Color.GRAY;
        this.ndBtnWall.getComponent(Sprite).color = Color.WHITE;
        this.ndBtnMartial.getComponent(Sprite).color = Color.GRAY;
    }

    onButtonOpenMartial(){
        console.log("打开兵种页")
        this.ndGeneral.active = false;
        this.ndWall.active = false;
        this.ndMartial.active = true;
        this.ndBtnGeneral.getComponent(Sprite).color = Color.GRAY;
        this.ndBtnWall.getComponent(Sprite).color = Color.GRAY;
        this.ndBtnMartial.getComponent(Sprite).color = Color.WHITE;
    }




}


